from .handle import handle

test_update_goods = {
        "goods_id": 0,
        "goods_title": "土鸡蛋20枚装",
        "imageContent": [
            "./resizeimg/5.jpg",
            "./resizeimg/4.jpg",
            "./resizeimg/3.jpg",
            "./resizeimg/1.jpg",
            "./resizeimg/2.jpg",
        ],
        "status": 1,
        "update_time": None,
        "imagesSlider": [
            "./resizeimg/6.jpg",
            "./resizeimg/7.jpg",
            "./resizeimg/8.jpg",
            ],
        "defaultImgIndex": 0,
        "price": 100,
        "sales": 600,
        "stock": 2179,
        "needJoin": 20,  # 需要拼单的数量
        "buy_type": 1,  # 1正常，2团购
        "goods_type1": 1,  # 商品类型一级分类
        "goods_type2": 2,   # 商品类型二级分类
        "baseCount": 50,   # 分销基本数量
        "discount": 0.5,  # 折扣
}


class goodsHandle(handle):
    def __init__(self):
        super(goodsHandle, self).__init__()

    def updateGoods(self, data):
        sliderimg_paths = data["imagesSlider"]
        contentimg_paths = data["imageContent"]
        temp = sliderimg_paths + contentimg_paths
        files = self._getFile(temp)

        del data["imagesSlider"]
        del data["imageContent"]
        data["sliderCount"] = sliderimg_paths.__len__()
        data["contentCount"] = contentimg_paths.__len__()
        url = self._base_url + "update_goods"

        return self._handle(url=url, data=data, files=files)

    def goods_sout_out(self, goods_id):
        data = {
            "goods_id": goods_id
        }
        url = self._base_url + "goods_sold_out"

        return self._handle(url=url, data=data)

    def goods_del(self, goods_id):
        data = {
            "goods_id": goods_id
        }
        url = self._base_url + "goods_del"

        return self._handle(url=url, data=data)

    def goods_show(self):
        url = self._base_url + "goods_show"

        return self._handle(url=url, htype="GET")

    def addCls(self, goods_type1, goods_type2, img_path):
        files = {"file": open(img_path, "rb")}
        data = {
            "goods_type1": goods_type1,
            "goods_type2": goods_type2,
            "create_by": "yixianshengji"
        }
        url = self._base_url + "upload_cls"

        return self._handle(url=url, data=data, files=files)

    def rmCls(self, goods_type1, goods_type2):
        data = {
            "goods_type1": goods_type1,
            "goods_type2": goods_type2,
            "create_by": "yixianshengji"
        }
        url = self._base_url + "upload_del"
        print(goods_type1, goods_type2)
        return self._handle(url=url, data=data)

    def showCls(self):
        url = self._base_url + "showcls"

        return self._handle(url=url, htype="GET")

def main():
    goodshandler = goodsHandle()
    res = goodshandler.updateGoods(test_update_goods)
    #res = goodshandler.goods_sout_out(5)
    #res = goodshandler.goods_del(1)
    #res = goodshandler.goods_show()
    #print(res["goodsList"].__len__())
    #print(res["goodsList"][0]["imageContent"].__len__())
    #img = cv2.imdecode(np.fromstring(b64decode(res["goodsList"][0]["imageContent"][4]), np.uint8), 1)
    #cv2.imshow("", img)
    #cv2.waitKey(0)
    print(res)

if __name__ == "__main__":
    main()
